import React from 'react';

import {
  BrowserRouter as Router,
  NavLink,
  Redirect,
  Route,
  Switch,
} from 'react-router-dom';
import './base.css';

export default class App extends React.Component {
  render() {
    return (
      <Router>
        <div>
          <h1>app组件</h1>
          <NavLink activeClassName="xxx" to="/home">
            调到Home
          </NavLink>
          <br />
          <NavLink exact to="/my">
            调到我的音乐
          </NavLink>
          <br />
          <NavLink to="/friend">调到朋友</NavLink>
          <Switch>
            <Redirect from="/" to="/home" exact />
            <Route path="/home" component={Home} />
            {/* 1. 设置path为动态路由, path="/路径/:自定义属性名" */}
            <Route path="/my/:xxx" component={MyMusic} />
          </Switch>
        </div>
      </Router>
    );
  }
}

function Home({ history }) {
  return (
    <>
      <h1>我是首页组件</h1>
      <button
        onClick={() => {
          history.goBack(); // 等价于go(-1); 回退一步
        }}
      >
        点我调到音乐
      </button>
    </>
  );
}

function MyMusic({ match }) {
  //2.  通过props.match.params.自定义属性名接收数据
  return <h1>我是我的音乐件 - {match.params.xxx}</h1>;
}
